package com.ts.core.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.api.R;
import com.ts.core.entity.Dict;
import com.ts.core.entity.dto.ExcelDictDTO;
import com.ts.core.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "字典管理")
@RestController
@RequestMapping("/admin/core/dict")
public class adminDictController {
    @Resource
    private DictService dictService;

    @ApiOperation("获取全部数据")
    @GetMapping("/list")
    public Map<String,Object> getAll()
    {
        List<Dict> list = dictService.list();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code","0");
        map.put("message","成功");
        HashMap<String, Object> data = new HashMap<>();
        data.put("list",list);
        map.put("data",data);
        return map;
    }
    @ApiOperation("根据上级id获取子节点数据列表")
    @GetMapping("/listByParentId/{parentId}")
    public Map<String,Object> listByParentId(
            @PathVariable Long parentId
    ){
      List<Dict> dictList= dictService.listByParentId(parentId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code","0");
        map.put("message","获取字典成功");
        HashMap<String, Object> data = new HashMap<>();
        data.put("list",dictList);
        map.put("data",data);
        return map;
    }

    @ApiOperation("Excel数据的批量导入")
    @PostMapping("/import")
    public Map<String,Object> batchImport(
            @ApiParam(value="Excel数据文件字典",required = true)
                    MultipartFile file
    ){

        try {
            InputStream inputStream = file.getInputStream();
            dictService.importData(inputStream);

        } catch (IOException e) {
            e.printStackTrace();
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("messsage","数据字典批量导入成功");
        return map;
    }

    @ApiOperation("数据字典的导出")
    @GetMapping("/export")
    public void Dictexport(HttpServletResponse response){
        try {

//            URLEncoder.encode在对字符串进行转码时，会将空格转为”+“号，URLDecoder.decode方法再解码时会将加号"+"和"%20"都解码为空格。
//            表面上看起来没有什么问题，但是遇到上传文件时候，对于带有空格的文件，将文件名称URLEncoder.encode之后，拼接成完整的文件地址进行访问时，会提示文件不存在。
//            最好的处理方式是URLEncoder.encode在编码之后，调用replaceAll("\\+", "%20")，将所有的加号替换成%20。

            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");


            String fileName= URLEncoder.encode("mydict","UTF-8").replaceAll("\\+","20%");




            response.setHeader("Content-disposition","attachment;filename="+fileName+".xlsx");
            EasyExcel.write(response.getOutputStream(), ExcelDictDTO.class).sheet("数据字典").doWrite(dictService.listdata());

        } catch (Exception e) {
            e.printStackTrace();
        }


    }


}
